package com.chuanyang.driverh.mainmy;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;

import com.amap.api.maps.AMap;
import com.amap.api.maps.MapView;
import com.amap.api.maps.UiSettings;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.services.core.PoiItem;
import com.chuanyang.driverh.BaseApplication;
import com.chuanyang.driverh.R;
import com.chuanyang.driverh.amap.AMapRegeocodeSearchUtil;
import com.chuanyang.driverh.amap.AMapRegeocodeSearchUtil.OnRegeocodePoiListSuccessListener;
import com.chuanyang.driverh.amap.AMapUtil;
import com.chuanyang.driverh.amap.LocationEntity;
import com.chuanyang.driverh.base.BaseTitleActivity;
import com.chuanyang.driverh.mainmy.adapter.ListMapAddressAdapter;
import com.chuanyang.driverh.utils.StringUtil;
import com.chuanyang.driverh.utils.eventbus.AddAddrEvent;
import com.chuanyang.driverh.utils.eventbus.EventBusUtil;

import java.util.ArrayList;
import java.util.List;

public class SelectAddrAsMapActivity extends BaseTitleActivity implements OnRegeocodePoiListSuccessListener,
        AMap.OnCameraChangeListener,AdapterView.OnItemClickListener
{
	private MapView mapView;
	private AMap amap;
	private int zoom=16;
	private ListView lvAddrs;
	private ListMapAddressAdapter adapter;
	private ProgressBar pb;
	private AMapRegeocodeSearchUtil reGeocodeUtil;
	private int type;

	private LatLng latLng=new LatLng(119.388051,35.174727);

	private LocationEntity locationEntity;

	@Override
	protected int getContentResId() {
		return R.layout.my_act_select_addr_as_map;
	}

	@Override
	protected void initView(Bundle savedInstanceState) {
		locationEntity= BaseApplication.getInstance().getLocationEntity();
		if (locationEntity!=null && locationEntity.getLongitude()!=0){
			latLng=new LatLng(locationEntity.getLatitude(),locationEntity.getLongitude());
		}else {
			BaseApplication.getInstance().startLoc();
		}
		mapView=(MapView)findViewById(R.id.map);
		mapView.onCreate(savedInstanceState);
		if(amap==null){
			amap=mapView.getMap();
			setMapUI();
			AMapUtil.moveToLatLng(amap,latLng,zoom);
		}
		lvAddrs=(ListView)findViewById(R.id.lv_addrs);
		pb=(ProgressBar)findViewById(R.id.progressBar1);
	}

	private void setMapUI(){
		UiSettings uiSettings = amap.getUiSettings();
		uiSettings.setScaleControlsEnabled(false);  //不显示比例尺amap
		uiSettings.setZoomControlsEnabled(true);
		uiSettings.setTiltGesturesEnabled(false);   //关闭手势倾斜和滑动
		uiSettings.setRotateGesturesEnabled(false);
	}

	@Override
	protected void initData() {
		type=getIntent().getIntExtra("type",1);
		setBackImg();
		getTitleTextView().setText("地图选点");
		reGeocodeUtil=new AMapRegeocodeSearchUtil(this);
		reGeocodeUtil.initGeoCodeSearch();
		adapter=new ListMapAddressAdapter(this, new ArrayList<PoiItem>());
		lvAddrs.setAdapter(adapter);
	}

	@Override
	protected void initListener() {
		findViewById(R.id.iv_my_location).setOnClickListener(this);
		amap.setOnCameraChangeListener(this);//地图状态改变的事件监听
		lvAddrs.setOnItemClickListener(this);
		reGeocodeUtil.setOnRegeocodePoiListSuccessListener(this); //反编译的回调监听
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		LocationEntity entity= AMapUtil.poiItemToLocationEntity(activityContext,
				(PoiItem) adapter.getItem(position));
		entity.setProvince(province);
		entity.setCity(StringUtil.isBlank(city)?province:city);
		entity.setDistrict(district);
		EventBusUtil.postEvent(new AddAddrEvent(entity,type));
		onBackPressed();
	}

	@Override
	public void onCameraChange(CameraPosition cameraPosition) {

	}

	@Override
	public void onCameraChangeFinish(CameraPosition position) {
		adapter.clear();
		pb.setVisibility(View.VISIBLE);
		reGeocodeUtil.startRecoderSearch(position.target);

	}

	private String province;
	private String city;
	private String district;
	@Override
	public void onRegeocodePoiListSuccess(List<PoiItem> items, String pro, String city, String region) {
		pb.setVisibility(View.GONE);
		adapter.add(items);
		this.province=pro;
		this.city=city;
		this.district=region;
	}
	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
			case R.id.iv_my_location:
				AMapUtil.moveToLatLng(amap,new LatLng(locationEntity.getLatitude(),locationEntity.getLongitude()),zoom);
				break;

			default:
				break;
		}
	}

	//地图的生命周期方法
	@Override
	protected void onResume() {
		super.onResume();
		mapView.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
		mapView.onPause();
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		mapView.onSaveInstanceState(outState);
	}

	@Override
	protected void onDestroy() {
		mapView.onDestroy();
		super.onDestroy();
	}



	/*
    boolean myLoc;
    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
		if (aMapLocation.getLatitude()!=0){
			latLng=new LatLng(aMapLocation.getLatitude(),aMapLocation.getLongitude());
			if (!myLoc){
				myLoc=true;
				AMapUtil.moveToLatLng(amap,latLng,zoom);
			}
		}
    }*/
}
